<?php
/**
 * 测试控制器
 */

namespace app\api\controller;

use app\api\exception\ApiServiceException;
use app\api\service\TestService;
use app\common\model\Compute;
use app\common\model\ComputeDetail;
use app\common\model\Dose;
use app\common\model\Meal;
use app\common\model\Menu;
use app\common\model\Order;
use app\common\model\Quantify;
use app\common\model\Type;
use app\common\validate\TestValidate;
use think\response\Json;

class TestController extends ApiBaseController {

	protected array $loginExcept = [
		'api/test/index',
	];

	/**
	 * 列表
	 * @param TestService $service
	 * @return Json
	 */
	public function index(TestService $service) {
		$order_list = Order::where('status', 0)->select();
		$meal_list = [];
		foreach ($order_list as $order_key => $order_value) {
			$dose_list = Dose::where('order_id', $order_value['id'])->select();
			foreach ($dose_list as $dose_key => $dose_value) {
				foreach ($dose_value['detail'] as $detail_key => $detail_value) {

					$meal_list[] = [
						'meal_id' => $detail_value['meal_id'],
						'num' => $detail_value['num'],
						'school_id' => $order_value['school_id'],
						'order_id' => $order_value['id'],
					];
				}
			}
			Order::where('id', $order_value['id'])->update(['status' => 1]);
		}

		$menu_list = [];
		$type_list = [];
		foreach ($meal_list as $meal_key => $meal_value) {

			$meal = Meal::where('id', $meal_value['meal_id'])->findOrEmpty();
			if (!$meal->isEmpty()) {
				foreach ($meal['detail'] as $meal_detail_key => $meal_detail_value) {
					$menu_list[] = $meal_detail_value['menu_id'];
					$menu = Menu::where('id', $meal_detail_value['menu_id'])->findOrEmpty();
					if (!$menu->isEmpty()) {
						foreach ($menu['detail'] as $menu_detail_key => $menu_detail_value) {
							$type_list[] = [
								'type_id' => $menu_detail_value['type_id'],
								'num' => $meal_value['num'] * $menu_detail_value['num'],
								'spec_id' => $menu_detail_value['spec_id'],
								'brand_id' => $menu_detail_value['brand_id'],
								'school_id' => $meal_value['school_id'],
								'order_id' => $meal_value['order_id'],
							];
						}
					}
				}
			}
		}

		$type_list2 = $this->array_do($type_list);
		$type_list3 = $this->array_do2($type_list);
		$start = date('Y-m-d', strtotime('next monday'));
		$end = date('Y-m-d', strtotime('next sunday'));
		$quantify = Quantify::create(['name' => $start . '-' . $end . '计算结果']);
		foreach ($type_list2 as $key => $value) {
			$type = Type::where('id', $value['type_id'])->findOrEmpty();
			if (!$type->isEmpty()) {
				$compute_data = [
					'quantify_id' => $quantify->id,
					'parent_id' => $type['parent_id'],
					'type_id' => $value['type_id'],
					'spec_id' => $value['spec_id'],
					'brand_id' => $value['brand_id'],
					'code' => date('YmdHis') . rand(00000, 99999),
					'all_num' => $value['num'],
				];
				$compute = Compute::create($compute_data);
				foreach ($type_list3 as $tk => $tv) {
					$compute_detail_data = [
						'user_id' => 0,
						'compute_id' => $compute->id,
						'order_id' => $tv['order_id'],
						'school_id' => $tv['school_id'],
						'type_id' => $tv['type_id'],
						'spec_id' => $tv['spec_id'],
						'brand_id' => $tv['brand_id'],
						'num' => $tv['total_num'],
					];
					$compute_detail = ComputeDetail::create($compute_detail_data);
				}
			}
		}

	}
	public function array_do($array) {
		$result = [];

		foreach ($array as $item) {
			$type_id = $item['type_id'];
			if (!isset($result[$type_id])) {
				$result[$type_id] = $item;
			} else {
				$result[$type_id]['num'] += $item['num'];
			}
		}

		$result = array_values($result);
		return $result;
	}
	public function array_do2($data) {
		$groupedData = [];

		foreach ($data as $item) {
			$school_id = $item['school_id'];
			$type_id = $item['type_id'];
			$spec_id = $item['spec_id'];
			$brand_id = $item['brand_id'];
			$order_id = $item['order_id'];
			$key = $school_id . '-' . $type_id;

			if (!isset($groupedData[$key])) {
				$groupedData[$key] = [
					'school_id' => $school_id,
					'type_id' => $type_id,
					'spec_id' => $spec_id,
					'brand_id' => $brand_id,
					'order_id' => $order_id,
					'total_num' => 0,
				];
			}

			$groupedData[$key]['total_num'] += $item['num'];
		}
		$result = array_values($groupedData);
		return $result;
	}
	/**
	 * 添加
	 *
	 * @param TestValidate $validate
	 * @param TestService $service
	 * @return Json
	 */
	public function add(TestValidate $validate, TestService $service): Json {
		$check = $validate->scene('api_add')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		$result = $service->createData($this->param);

		return $result ? api_success() : api_error();
	}

	/**
	 * 详情
	 *
	 * @param TestValidate $validate
	 * @param TestService $service
	 * @return Json
	 */
	public function info(TestValidate $validate, TestService $service): Json {
		$check = $validate->scene('api_info')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {

			$result = $service->getDataInfo($this->id);
			return api_success([
				'user_level' => $result,
			]);

		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 修改
	 * @param TestService $service
	 * @param TestValidate $validate
	 * @return Json
	 */
	public function edit(TestService $service, TestValidate $validate): Json {
		$check = $validate->scene('api_edit')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->updateData($this->id, $this->param);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 删除
	 * @param TestService $service
	 * @param TestValidate $validate
	 * @return Json
	 */
	public function del(TestService $service, TestValidate $validate): Json {
		$check = $validate->scene('api_del')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->deleteData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}
}
